#include <stdbool.h>
#include <stdlib.h>
#include "SqliteInterface.h"
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include "../RealtimeWaveform.h"
#include "../ErrorLog.h"
#include "../modbus/create_modbus_custom_slave.h"
#include "elecQuttFunction.h"
#define ELEC_QUTT_SQL_TABLE "ElecQuttTable"

static int elecQuttCallback(void *para, int argc, char **argv, char **azColName){

    struct elecQuttDataDB* peqData = (struct elecQuttDataDB*)para;
    
    for(int i = 0; i < argc; i++){
        if(strcmp(azColName[i], "year") == 0){
            peqData->year = atoi(argv[i]);
        }
        if(strcmp(azColName[i], "month") == 0){
            peqData->month = atoi(argv[i]);
        }
        if(strcmp(azColName[i], "day") == 0){
            peqData->day = atoi(argv[i]);
        }
        if(strcmp(azColName[i], "hour") == 0){
            peqData->hour = atoi(argv[i]);
        }
        if(strcmp(azColName[i], "totalElecData") == 0){
            peqData->totalElecData = atof(argv[i]);
        }
        if(strcmp(azColName[i], "yearElecData") == 0){
            peqData->yearElecData = atof(argv[i]);
        }
        if(strcmp(azColName[i], "monthElecData") == 0){
            peqData->monthElecData = atof(argv[i]);
        }
        if(strcmp(azColName[i], "dayElecData") == 0){
            peqData->dayElecData = atof(argv[i]);
        }
        if(strcmp(azColName[i], "hourElecData") == 0){
            peqData->hourElecData = atof(argv[i]);
        }   
    }

    return 0;
}

//查询电量数据
int readElecQuttData_db(struct elecQuttDataDB* peqdata, queryDataType_t queryType){

    char *zErrMsg = 0;
    int rc;

    char sql[200];
    if(queryType == ELEC_QUTT_DATA_RESTORE){
        sprintf(sql, "SELECT * FROM %s WHERE isDiscOrChrg='%s' AND isAcOrBat='%s' ORDER BY totalElecData DESC LIMIT 1;", ELEC_QUTT_SQL_TABLE, peqdata->chargeType, peqdata->branchType);
    }
    else if(queryType == ELEC_QUTT_DATA_HOUR){
        sprintf(sql, "SELECT * FROM %s WHERE isDiscOrChrg='%s' AND isAcOrBat='%s' AND year=%d AND month=%d AND day=%d AND hour=%d;", 
                ELEC_QUTT_SQL_TABLE, peqdata->chargeType, peqdata->branchType, peqdata->year, peqdata->month, peqdata->day, peqdata->hour);
    }
    else if(queryType == ELEC_QUTT_DATA_DAY){
        sprintf(sql, "SELECT * FROM %s WHERE isDiscOrChrg='%s' AND isAcOrBat='%s' AND year=%d AND month=%d AND day=%d ORDER BY totalElecData DESC LIMIT 1;", 
                ELEC_QUTT_SQL_TABLE, peqdata->chargeType, peqdata->branchType, peqdata->year, peqdata->month, peqdata->day);
    }
    else if(queryType == ELEC_QUTT_DATA_MONTH){
        sprintf(sql, "SELECT * FROM %s WHERE isDiscOrChrg='%s' AND isAcOrBat='%s' AND year=%d AND month=%d ORDER BY totalElecData DESC LIMIT 1;", 
                ELEC_QUTT_SQL_TABLE, peqdata->chargeType, peqdata->branchType, peqdata->year, peqdata->month);
    }
    else if(queryType == ELEC_QUTT_DATA_YEAR){
        sprintf(sql, "SELECT * FROM %s WHERE isDiscOrChrg='%s' AND isAcOrBat='%s' AND year=%d ORDER BY totalElecData DESC LIMIT 1;", 
                ELEC_QUTT_SQL_TABLE, peqdata->chargeType, peqdata->branchType, peqdata->year);
    }
    else{
        printf("查询类型错误\n");
        return -1;
    }
    
    rc = sqlite3_exec(db, sql, elecQuttCallback, peqdata, &zErrMsg);
    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        return -1;
    } else {
        // fprintf(stdout, "readElecQuttData_db Data retrieved successfully\n");
    }
    return 0;

}

int deleteElecQuttData_db(){

    char *zErrMsg = 0;
    int rc;
    
    char sql[50];
    sprintf(sql, "DELETE FROM ElecQuttTable;");
   
    rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        return -1;
    } else {
        fprintf(stdout, "Data deleted successfully\n");
        return 0;
    }
}

int insertElecQuttData_db(int year, int month, int day, int hour, char* isDiscOrChrg, char* isAcOrBat, 
                          float totalElecData, float yearElecData, float monthElecData, float dayElecData, float hourElecData){

    int ret;
    char *zErrMsg = 0;
    char sql[300];

    sprintf(sql,"INSERT INTO %s (year, month, day, hour, isDiscOrChrg, isAcOrBat, totalElecData, yearElecData, monthElecData, dayElecData, hourElecData) VALUES (%d, %d, %d, %d, '%s', '%s', %f, %f, %f, %f, %f);",
            ELEC_QUTT_SQL_TABLE, year, month, day, hour, isDiscOrChrg, isAcOrBat, totalElecData, yearElecData, monthElecData, dayElecData, hourElecData);

    ret = sqlite3_exec(db, sql, 0, 0, &zErrMsg);

    if (ret != SQLITE_OK)
    {
        printf(" __FUNCTION__: %s ,__LINE__: %d sqlite3_exec SQL error, %s\n", __func__, __LINE__,zErrMsg);
        sqlite3_free(zErrMsg);
        return -1;
    }
    else
    {
        printf("elec qutt data (%d, %d, %d, %d, '%s', '%s', %f, %f, %f, %f, %f) is successfully inserted.\n",
            year, month, day, hour, isDiscOrChrg, isAcOrBat, totalElecData, yearElecData, monthElecData, dayElecData, hourElecData);
    }
    return SQLITE_OK;   
}

